package com.tanhua.server.controller;

import com.tanhua.domain.vo.*;
import com.tanhua.server.service.CommentService;
import com.tanhua.server.service.MomentService;
import com.tanhua.server.service.RecommendUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 圈子模块--消费者
 */
@RestController
@RequestMapping("/movements")
public class MomentController {
    @Autowired
    private MomentService momentService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private RecommendUserService recommendUserService;


    /**
     * 发布动态
     *
     * @param publishVo
     * @param imageContent
     * @return
     */
    @PostMapping
    public ResponseEntity postMoment(PublishVo publishVo, MultipartFile[] imageContent) {
        momentService.postMoment(publishVo, imageContent);
        return ResponseEntity.ok(null);

    }


    /**
     * 朋友动态
     *
     * @return
     */
    @GetMapping()
    public ResponseEntity queryFriendPublishList(@RequestParam(defaultValue = "1") Long page, @RequestParam(defaultValue = "10") Long pagesize) {

        PageResult<MomentVo> pageResult = momentService.queryFriendPublishList(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 推荐动态
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity queryRecommendPublishList(@RequestParam(defaultValue = "1") Long page, @RequestParam(defaultValue = "10") Long pagesize) {
        PageResult<PublishVo> result = momentService.queryRecommendPublishList(page,pagesize);
        return ResponseEntity.ok(result);
    }

    /**
     * 用户动态
     * @param page
     * @param pagesize
     * @param userId ： 后台管理系统中使用
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity queryMyPublishList(@RequestParam(defaultValue = "1") Long page, @RequestParam(defaultValue = "10") Long pagesize,@RequestParam Long userId){

        PageResult<MomentVo> pageResult = momentService.queryMyPublishList(page,pagesize,userId);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 动态 点赞
     * @param publishId
     * @return
     */
    @GetMapping("/{publishId}/like")
    public ResponseEntity like(@PathVariable String publishId){
        long likeCount = commentService.like(publishId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 动态取消点赞
     * @param publishId
     * @return
     */
    @GetMapping("/{publishId}/dislike")
    public ResponseEntity dislike(@PathVariable String publishId){
        long likeCount = commentService.dislike(publishId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 动态 喜欢
     * @param publishId
     * @return
     */
    @GetMapping("/{publishId}/love")
    public ResponseEntity love(@PathVariable String publishId){
        long loveCount = commentService.love(publishId);
        return ResponseEntity.ok(loveCount);
    }

    /**
     * 动态取消喜欢
     * @param publishId
     * @return
     */
    @GetMapping("/{publishId}/unlove")
    public ResponseEntity unlove(@PathVariable String publishId){
        long loveCount = commentService.unlove(publishId);
        return ResponseEntity.ok(loveCount);
    }

    /**
     * 谁看过我
     *
     * @return
     */
    @GetMapping("/visitors")
    public ResponseEntity visitors(){
        List<VisitorVo> voList = momentService.queryVisitors();
        return ResponseEntity.ok(voList);
    }

    /**
     * 通过id 查询单条动态
     *
     * 这个api与最近访客(/visitors)功能冲突
     *
     * @param publishId
     * @return
     */
    @GetMapping("/{publishId}")
    public ResponseEntity findById(@PathVariable String publishId){
        MomentVo vo = momentService.findById(publishId);
        return ResponseEntity.ok(vo);
    }


}
